//
//  PreferredHeaderView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/16.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "PreferredHeaderView.h"

#import "MainPageHeaderModel.h"

#import "ShopHeaderBannerView.h"

#import "HeaderEntryCell.h"

@interface PreferredHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) ShopHeaderBannerView *bannerView;

@property (nonatomic,strong) UIImageView *bannerMengbanV;

@property (nonatomic,strong) UICollectionView *collectionV;

@end

@implementation PreferredHeaderView

#pragma mark - setter

- (void)setGoodTypesArr:(NSMutableArray *)goodTypesArr{
    _goodTypesArr = goodTypesArr;
    [self.collectionV reloadData];
}

- (void)setBannerSourceArr:(NSMutableArray *)bannerSourceArr{
    _bannerSourceArr = bannerSourceArr;
    self.bannerView.sourceArr = _bannerSourceArr;
    if (_bannerSourceArr.count <= 0) {
        self.bannerView.hidden = YES;
        self.bannerMengbanV.frame = self.bannerView.frame;
        [self addSubview:self.bannerMengbanV];
    }else{
        self.bannerView.hidden = NO;
        self.bannerMengbanV.hidden = YES;
    }
}
//轮播图蒙版
- (UIImageView *)bannerMengbanV{
    if (!_bannerMengbanV) {
        _bannerMengbanV = [[UIImageView alloc] initWithImage:LoadImage(@"首页_banner")];
    }
    return _bannerMengbanV;
}

-(id)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.whiteColor;
        self.goodTypesArr = [NSMutableArray array];
        [self initUI];
    }
    return self;
}

-(void)initUI{
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth*0.4)];
    imgV.image = LoadImage(@"首页_顶部背景");
    [self addSubview:imgV];
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, kHeight_StatusBar + 10, ScreenWidth-80, 30)];
    tf.clipsToBounds = YES;
    tf.layer.cornerRadius = 15;
    tf.backgroundColor = RGBA(255, 255, 255, 0.8f);
    tf.placeholder = @"  请输入搜索内容";
    tf.leftView = [self leftView];
    tf.leftViewMode = UITextFieldViewModeAlways;
    [self addSubview:tf];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(ScreenWidth-54, tf.top, 30, 30);
    [menuBtn setImage:LoadImage(@"首页_消息") forState:normal];
    [menuBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [menuBtn addTarget:self action:@selector(showMenuClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:menuBtn];
    
    self.bannerView = [[ShopHeaderBannerView alloc] initWithFrame:CGRectMake(10, tf.bottom+15, ScreenWidth-20, (ScreenWidth-20)*0.38)];
    self.bannerView.sourceArr = self.bannerSourceArr;
    [self addSubview:self.bannerView];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth-20)/5-10, 60);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(10, self.bannerView.bottom+15, ScreenWidth-20, 60) collectionViewLayout:layout];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.backgroundColor = UIColor.whiteColor;
    self.collectionV.showsVerticalScrollIndicator = NO;
    self.collectionV.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.collectionV];
    [self.collectionV registerClass:[HeaderEntryCell class] forCellWithReuseIdentifier:@"HeaderEntryCell"];
    
    UIImageView *inviteBgImgV = [[UIImageView alloc] initWithFrame:CGRectMake(12, self.collectionV.bottom+25, ScreenWidth-24, (ScreenWidth-24)/7)];
    inviteBgImgV.image = LoadImage(@"指南商城_邀请好友背景");
    [self addSubview:inviteBgImgV];
    
    UIImageView *inviteImgV = [[UIImageView alloc] initWithFrame:CGRectMake(inviteBgImgV.right-((inviteBgImgV.height+20)*6/7)-10, inviteBgImgV.top-15, (inviteBgImgV.height+20)*6/7, inviteBgImgV.height+20)];
    inviteImgV.image = LoadImage(@"指南商城_邀请好友");
    [self addSubview:inviteImgV];
    
    UIView *leftLineV = [[UIView alloc] initWithFrame:CGRectMake(10, inviteBgImgV.bottom+30, 4, 18)];
    leftLineV.backgroundColor = RGB(37, 130, 250);
    leftLineV.clipsToBounds = YES;
    leftLineV.layer.cornerRadius = 2;
    [self addSubview:leftLineV];
    
    UILabel *leftLB = [[UILabel alloc] initWithFrame:CGRectMake(leftLineV.right+5, leftLineV.top-6, 100, 30)];
    leftLB.textColor = MainBlackTextColor;
    leftLB.font = FontSetWithWeight(22, 0.3f);
    leftLB.text = @"优选商品";
    [self addSubview:leftLB];
    
    UILabel *detailLB = [[UILabel alloc] initWithFrame:CGRectMake(leftLB.right+10, leftLB.top+6, ScreenWidth/2+30, 18)];
    detailLB.textColor = MainPureColor(153);
    detailLB.font = FontSet(13);
    detailLB.text = @"为您千挑百选 只为精益圆满";
    [self addSubview:detailLB];
    
    UIImageView *moreImgV = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 50, detailLB.top, 18, 18)];
    moreImgV.image = LoadImage(@"指南商城_更多");
    [self addSubview:moreImgV];
    self.height = leftLineV.bottom+30;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.updateHeightBlock) {
            self.updateHeightBlock(self.height);
        }
    });
}


#pragma mark - UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((ScreenWidth-20)/5-10, 60);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.goodTypesArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MainPageHeaderModel *model = self.goodTypesArr[indexPath.row];
    static NSString * CellIdentifier = @"HeaderEntryCell";
    HeaderEntryCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.bannerTouchBlock) {
        self.bannerTouchBlock(indexPath.row);
    }
}

#pragma mark -
-(UIView *)leftView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    UIImageView *searchImgV = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 15, 15)];
    searchImgV.image = LoadImage(@"首页_搜索");
    [view addSubview:searchImgV];
    return view;
}

-(void)showMenuClick{
}

@end
